package com.cskaoyan.controller;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.params.AdminInsertBO;
import com.cskaoyan.bean.params.AdminListBO;
import com.cskaoyan.bean.params.BaseParam;
import com.cskaoyan.service.AuthService;
import com.cskaoyan.service.LogService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.System;
import java.util.Date;

/**
 * @USER: cuiweiguo#
 * @DATE: 2022/5/5
 * @TIME: 20:49
 * @PROJECT_NAME: mallproject2
 */
@RestController
@RequestMapping("admin/admin")
public class AdminController {

    @Autowired
    AuthService authService;

    @GetMapping("list")
    public AdminListRespVo<AdminListBean> list(String username, BaseParam baseParam) {
        //GET http://182.92.235.201:8083/admin/admin/list?page=1&limit=20&sort=add_time&order=desc HTTP/1.1
        //查询数据库
        AdminListBean adminListBean = authService.queryAdminList(baseParam, username);
        return AdminListRespVo.listOk(adminListBean);
    }

    @Autowired
    LogService logService;
    @PostMapping("delete")
    public AdminListRespVo<String> delete(@RequestBody AdminListBO adminListBO, HttpServletRequest servletRequest) {
        int code = authService.deleteAdmin(adminListBO);
        if (code == 200) {
            //将操作写入日志
            Subject subject = SecurityUtils.getSubject();
            Integer userId = (Integer) subject.getPrincipals().getPrimaryPrincipal();
            //数据库查询username
            String username = authService.queryUsernameById(userId);
            String ip = servletRequest.getRemoteAddr();
            String admin = username;
            Integer type = 1;
            String action = "删除管理员";
            Boolean status = true;
            String result = "ok";
            Log log = new Log(null, admin, ip, type, action, status, result, null, new Date(System.currentTimeMillis()),
                    new Date(System.currentTimeMillis()), null);
            //将log写入日志
            logService.insertLog(log);
            return AdminListRespVo.listOk("null");
        }
        return null;
    }

    @PostMapping("update")
    public AdminListRespVo<String> create(@RequestBody AdminUpdateBO adminUpdateBO,HttpServletRequest servletRequest) {
        //TODO:修改上传图片业务
        //完成上传admin业务
        // 返回插入数据的详细信息
        AdminUpdatePO admin = authService.updateAdmin(adminUpdateBO);
        //将操作写入日志
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipals().getPrimaryPrincipal();
        //数据库查询username
        String username = authService.queryUsernameById(userId);
        String ip = servletRequest.getRemoteAddr();
        String adminName = username;
        Integer type = 1;
        String action = "删除管理员";
        Boolean status = true;
        String result = "ok";
        Log log = new Log(null, adminName, ip, type, action, status, result, null, new Date(System.currentTimeMillis()),
                new Date(System.currentTimeMillis()), null);
        //将log写入日志
        logService.insertLog(log);
        return AdminListRespVo.listOk("admin");
        //return null;
    }

    @PostMapping("create")
    public AdminListRespVo<String> update(@RequestBody AdminInsertBO adminInsertBO,HttpServletRequest servletRequest) {
        //上传图片业务已经完成
        //完成上传admin业务
        // 返回插入数据的详细信息
        AdminCreatePO admin = authService.insertAdmin(adminInsertBO);
        //将操作写入日志
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipals().getPrimaryPrincipal();
        //数据库查询username
        String username = authService.queryUsernameById(userId);
        String ip = servletRequest.getRemoteAddr();
        String adminName = username;
        Integer type = 1;
        String action = "添加管理员";
        Boolean status = true;
        String result = "ok";
        Log log = new Log(null, adminName, ip, type, action, status, result, null, new Date(System.currentTimeMillis()),
                new Date(System.currentTimeMillis()), null);
        //将log写入日志
        logService.insertLog(log);

        return AdminListRespVo.listOk("admin");
        //return null;
    }

}
